const express = require('express');
const pro = express.Router();
const pool = require('../pool.js');
const fs = require('fs');
// 登陆显示店铺商品的信息
pro.get('/shop', (req, res) => {
    let page = (parseInt(req.query.page) - 1) * 5;
    let length = (parseInt(req.query.page)) * 5;
    let kind;
    if (req.query.kind == 1) {
        kind = '上衣';
    } else if (req.query.kind == 2) {
        kind = '下装';
    } else if (req.query.kind == 3) {
        kind = '鞋类';
    } else {
        kind = "饰品";
    };
    let title = '%' + req.query.title + '%';
    let url = 'select * from goods';
    if (req.query.title) {
        pool.query(`${url} where title like ? and shopId = ? limit ?,?`, [title, req.query.shopId, page, length], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    } else if (req.query.kind) {
        pool.query(`${url} where kind = ? and title like ? and shopId = ? limit ?,?`, [kind, title, req.query.shopId, page, length], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    } else {
        let state, stat;
        if (req.query.state == 1) {
            state = '上架中';
        } else if (req.query.state == 2) {
            state = '审核中';
            stat = '审核不通过';
        } else {
            state = '已下架'
        };
        pool.query(`${url} where shopId = ? and state = ? or shopId = ? and state = ? limit ?,?`, [req.query.shopId, state, req.query.shopId, stat, page, length], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    }
});
// 删除商品或订单
pro.delete('/delete', (req, res) => {
    let id = req.query.id;
    let img = req.query.img.split(',');
    for (const key of img) {
        fs.unlinkSync(`${__dirname}/components/commodity/${key}`);
    };
    pool.query('delete from goods where id=?', [id], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    });
})
// 下架商品
pro.get('/putdown', (req, res) => {
    let state = '已下架';
    pool.query('update goods set state=? where id=?', [state, req.query.id], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 订单管理
let order = 'select * from orders';
pro.get('/orders', (req, res) => {
    let order_status = '待发货';
    if (req.query.oid) {
        // 如果输入订单编号
        pool.query(`${order} where oid = ? and order_status = ?  and shopId = ?`, [req.query.oid, order_status, req.query.shopId], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    } else {
        pool.query(`${order} where order_status =? and shopId = ?`, [order_status, req.query.shopId], (err, r) => {
            if (err) {
                return;
            }
            res.send(r);
        })
    }
});
// 发货
pro.get('/outders', (req, res) => {
    let id = req.query.id;
    let order_status = '运输中';
    let out = '待发货';
    pool.query('update orders set order_status = ? where id = ? and order_status = ?', [order_status, id, out], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 售后
pro.get('/after', (req, res) => {
    let order_status = '待处理';
    pool.query(`${order} where order_status = ? and shopId = ?`, [order_status, req.query.shopId], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 总价echrats显示
pro.get('/total', (req, res) => {
    let state = '已完成';
    pool.query(`${order} where order_status = ? and shopId = ?`, [state, req.query.shopId], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 修改商品信息
pro.put('/update', (req, res) => {
    let { title, id, commodity, inventory, price, type } = req.body;
    let state = '审核中';
    pool.query('update goods set title=?,commodity=?,price=?,type=?,inventory=?,state=? where id=?', [title, commodity, price, type, inventory, state, id], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
module.exports = pro;
